<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../../../layuiadmin/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../../layuiadmin/style/admin.css" media="all">
		<link rel="stylesheet" href="../../../../css/common.css" media="all">

	</head>
	
	<style type="text/css">
		th,
		td{
			padding: .3rem;
			white-space: nowrap;
		}
		
		.table_box input{
			border: none;
			text-align: center;
		}
		
		
		.table_box input::-webkit-outer-spin-button,
		.table_box input::-webkit-inner-spin-button{
			-webkit-appearance: none;
		}
		
		.table_box input[type="number"]{
			-moz-appearance: textfield;
		}
		
		
		.addtext{
			display: block;
			margin: 0 auto;
			width: 20px;
			height: 20px;
			color: #FFF;
			line-height: 20px;
			background-color: #444;
			border-radius: 4px;
			cursor: pointer;
			text-align: center;
			user-select: none;
		}
		
		.deltext{
			display: block;
			margin: 0 auto;
			width: 20px;
			height: 20px;
			color: #FFF;
			line-height: 20px;
			background-color: #444;
			border-radius: 4px;
			cursor: pointer;
			text-align: center;
			user-select: none;
		}
		
		.deltext:last-child{
			display: block;
		}
	</style>
	
	<body>

		<div class="layui-fluid">
			<div class="layui-row" id="view">
				<form class="layui-form test_edit" lay-filter="test_detail">
					<div class="layui-card">
						<div class="layui-card-body">
							
							<div class="layui-form-item">
								<label class="layui-form-label"><text class='text_red_color'>*</text>产品类型：</label>
								<div class="layui-input-block product"></div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">持有人选择：</label>
								<div class="layui-input-inline">
									<input type="text" name="member_name"  placeholder="选择持有人选择" autocomplete="off" class="layui-input member_name">
									<input type="hidden" name="member_id"  autocomplete="off" class="layui-input member_id">
									<input type="hidden" name="user_id"  autocomplete="off" class="layui-input user_id">
								</div>
								<div class="layui-form-mid layui-word-aux">
									<a href="Javascript:;" onclick="member()">选择</a>
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">购买人选择：</label>
								<div class="layui-input-inline">
									<input type="text" name="buyer_name"  placeholder="选择购买人选择" autocomplete="off" class="layui-input buyer_name">
									<input type="hidden" name="buyer_id"  autocomplete="off" class="layui-input buyer_id">
								</div>
								<div class="layui-form-mid layui-word-aux">
									<a href="Javascript:;" onclick="buyer()">选择</a>
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">返现会员选择：</label>
								<div class="layui-input-inline">
									<input type="text" name="back_name"  placeholder="选择返现会员选择" autocomplete="off" class="layui-input back_name">
									<input type="hidden" name="back_memberid"  autocomplete="off" class="layui-input back_memberid">
								</div> 
								<div class="layui-form-mid layui-word-aux">
									<a href="Javascript:;" onclick="back()">选择</a>
								</div>
							</div>
							
							<div class="layui-form-item">
								<label class="layui-form-label"><text class='text_red_color'>*</text>供应商选择：</label>
								<div class="layui-input-inline">
									<input type="text" name="supplier_name"  placeholder="选择供应商选择" autocomplete="off" class="layui-input supplier_name">
									<input type="hidden" name="supplierid" lay-verify=”required” autocomplete="off" class="layui-input supplierid">
								</div> 
								<div class="layui-form-mid layui-word-aux">
									<a href="Javascript:;" onclick="supplier()">选择</a>
								</div>
							</div>
							
							<div class="layui-form-item">
								<label class="layui-form-label">品牌编号：</label>
								<div class="layui-input-block">
									<input type="text" name="brand_no" placeholder="请输入品牌编号" autocomplete="off"
									 class="layui-input" value = '100012465'>
								</div>
							</div>
							
							
							
							
							<div class="layui-form-item">
								<label class="layui-form-label">返现设置：</label>
								<div class="layui-input-block">
									<div class="form-one">
										<table class="table_box" border="1" style="width: 100%;text-align: center;">
											<tr>
												<th>阶段</th>
												<th>达标金额</th>
												<th>返现金额</th>
												<th>到期返现金额</th>
												<th>返现红包</th>
												<th>到期返现红包</th>
												<th>开始(天)</th>
												<th>结束(天)</th>
												<th>操作</th>
											</tr>
											<tr class="tableli1">
												<td>
													<input name="oid" type="number" disabled="disabled" autocomplete="off" class="layui-input" value="1">
												</td>
												<td>
													<input name="amount" type="number" placeholder="请输入达标金额" autocomplete="off" class="layui-input">
												</td>
												<td>
													<input name="back_amount" type="number" placeholder="请输入返现金额" autocomplete="off" class="layui-input">
												</td>
												<td>
													<input name="back_expire_fee" type="number" placeholder="请输入到期返现金额" autocomplete="off" class="layui-input">
												</td>
												<td>
													<input name="back_packet" type="number" placeholder="请输入返现红包" autocomplete="off" class="layui-input">
												</td>
												<td>
													<input name="back_expire_packet" type="number" placeholder="请输入到期返现红包" autocomplete="off" class="layui-input">
												</td>
												
												<td>
													<input name="startday" type="number" placeholder="请输入开始时间" autocomplete="off" class="layui-input" value="1">
												</td>
												<td>
													<input name="endday" type="number" placeholder="请输入截止时间" autocomplete="off" class="layui-input" value="30">
												</td>
												<td>
													<span class="addtext" id="table1">+</span>
												</td>
											</tr>
										</table>
									</div>
								</div>
							</div>
							
							
							<div class="layui-form-item">
								<label class="layui-form-label">返现到期<span style="color: red;">*</span>：</label>
								<div class="layui-input-block">
									<input type="text" name="back_expire" id="date" placeholder="返现到期" autocomplete="off" class="layui-input">
								</div>
							</div>
							
							<!-- <div class="layui-form-item">
								<label class="layui-form-label"><text class='text_red_color'>*</text>返现金额：</label>
								<div class="layui-input-block">
									<input type="text" name="back_amount" lay-verify="required" placeholder="请输入返现金额" autocomplete="off"
									 class="layui-input">
								</div>
							</div> -->
							
							
							<!-- <div class="layui-form-item">
								<label class="layui-form-label">购机红包：</label>
								<div class="layui-input-block">
									<input type="text" name="back_packet" placeholder="请输入购机红包" autocomplete="off" class="layui-input">
								</div>
							</div> -->
							
							
							
							
							
							
							<!-- <div class="layui-form-item">
								<label class="layui-form-label">到期购机红包：</label>
								<div class="layui-input-block">
									 <input type="text" name="back_expire_packet"   placeholder="到期购机红包" autocomplete="off" class="layui-input">
								</div>
							</div> -->
							<div class="layui-form-item">
								<label class="layui-form-label">服务费金额：</label>
								<div class="layui-input-block">
									 <input type="text" name="service_fee"   placeholder="到期购机红包" autocomplete="off" class="layui-input">
								</div>
							</div>
							
							<div class="layui-form-item">
								<label class="layui-form-label">机身编号：</label>
								<div class="layui-input-block">
									<input type="text" name="pos_sn_pre" placeholder="请输入支付公司及产品类型(例:K205T0H)" autocomplete="off"
									 class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<div class="layui-inline">
									<label class="layui-form-label">序列号范围：</label>
									<div class="layui-input-inline" style="width: 100px;">
										<input type="text" name="sn_start" placeholder="开始序列号" autocomplete="off" class="layui-input">
									</div>
									<div class="layui-form-mid">-</div>
									<div class="layui-input-inline" style="width: 100px;">
										<input type="text" name="sn_end" placeholder="结束序列号" autocomplete="off" class="layui-input">
									</div>
									<button type="button" class="layui-btn layui-btn-normal" onclick="createnum()">生成</button>
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">机具序列号：</label>
								<div class="layui-input-block">
									<textarea placeholder="请输入机具序列号" name="pos_sn" class="layui-textarea"></textarea>
								</div>
							</div>

							<!--<div class="layui-form-item">
								<label class="layui-form-label">入库备注：</label>
								<div class="layui-input-block">
									<textarea placeholder="请输入库备注" name="description" class="layui-textarea"></textarea>
								</div>
							</div>-->
						</div>
					</div>

					<div class="layui-form-item layui-layout-admin">
						<div class="layui-input-block">
							<div class="layui-footer" style="left: 0;">
								<!-- <button type="reset" class="layui-btn layui-btn-primary">重置</button> -->
								<button class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
							</div>
						</div>
					</div>
				</form>
			</div>
		</div>
		<script src="../../../../layuiadmin/layui/layui.js"></script>
		<script src="../../../../js/jquery.min.js"></script>
		<script src="../../../../js/jquery.jqprint-0.3.js"></script>
		<script src="../../../../js/common.js"></script>
		<script>
			var _data = {
				// id: getParam('id')
			}
			// console.log(_data.id);

			layui.config({
				base: '../../../../layuiadmin/' //静态资源所在路径
			}).extend({
				index: 'lib/index' //主入口模块
			}).use(['index', 'form', 'laytpl', 'laydate', 'layedit'], function() {
				var $ = layui.$,
					admin = layui.admin,
					layedit = layui.layedit,
					laytpl = layui.laytpl,
					laydate = layui.laydate,
					form = layui.form;
				
				var pos_sn_arr = [],
					img_arr1 = [],
					img_arr2 = [];

				var index = layedit.build('demo_editor', {
					tool: ['strong' //加粗
						, 'italic' //斜体
						, 'underline' //下划线
						, 'del' //删除线

						, '|' //分割线

						, 'left' //左对齐
						, 'center' //居中对齐
						, 'right' //右对齐
						, 'link' //超链接
						, 'unlink' //清除链接
						, 'face' //表情
					]
				});

				on_off_show('radio')
				initproductshow1('radio')
				if (_data.id) {
					initdata();
				}
				
				laydate.render({
				    elem: '#date'
				});
				window.member = function(type) {
					layer.open({
						type: 2,
						title: '选择',
						shadeClose: false,
						shade: 0.5,
						maxmin: true, //开启最大化最小化按钮
						area: ['800px', '800px'],
						content: 'member_information.html',
						end: function() {
						},
						success: function() {
						}
					});
				}	
				window.buyer = function(type) {
					layer.open({
						type: 2,
						title: '选择',
						shadeClose: false,
						shade: 0.5,
						maxmin: true, //开启最大化最小化按钮
						area: ['800px', '800px'],
						content: 'buyer_information.html',
						end: function() {
						},
						success: function() {
						}
					});
				}	
				window.back = function(type) {
					layer.open({
						type: 2,
						title: '选择',
						shadeClose: false,
						shade: 0.5,
						maxmin: true, //开启最大化最小化按钮
						area: ['800px', '800px'],
						content: 'back_information.html',
						end: function() {
						},
						success: function() {
						}
					});
				}	
				
				window.supplier = function(type) {
					layer.open({
						type: 2,
						title: '选择供应商',
						shadeClose: false,
						shade: 0.5,
						maxmin: true, //开启最大化最小化按钮
						area: ['800px', '800px'],
						content: 'supplier_information.html',
						end: function() {
						},
						success: function() {
						}
					});
				}	
				
				//监听提交
				form.on('submit(demo1)', function(data) {
					
					var data = data.field;
					_data = Object.assign(_data, data); 
					// console.log(_data.product);
					// _data = Object.assign(_data, data);
					// console.log(data.pos_sn.replace(/[\r\n]/g,""));
					pos_sn_arr = [];
					var pos_sn = data.pos_sn.replace(/[\r\n]/g,"").split(',')
					for (var i = 0; i < pos_sn.length; i++) {
						if(pos_sn[i]){
							pos_sn_arr.push({
								'pos_sn': pos_sn[i].replace(/ /g,'')
							})
						}
					}
					_data.pos_data = JSON.stringify(pos_sn_arr);
					
					
					
					layer.confirm('确定提交数据？', function() {
						loading();
						if (!_data.id) {
							submitdata(server.pos_insert);
						} else {
							submitdata();
						}
					}, function() {
						layer.msg('取消操作');
					});
					return false;
				});

				function submitdata(_url) {
					$.fn.serializeObject = function() {
						var o = {};  
						var a = $(this).find('input')
						// var a = this.serializeArray();
						$.each(a, function() {
							if (o[this.name]) {  
								if (!o[this.name].push) {  
									o[this.name] = [ o[this.name] ];  
								}  
								o[this.name].push(this.value || '');  
							} else {  
								o[this.name] = this.value || '';  
							}  
						});
						return o;
					}
					var json = [];
					$(".tableli1").each(function(){
						json.push($(this).serializeObject())
					})
					if(json.length != 0){
						var back_amounts = [];
						var back_expire_fees = [];
						var back_packets = [];
						var back_expire_packets = [];
						
						for(let i=0;i<json.length;i++){
							back_amounts.push(json[i].back_amount)
							back_expire_fees.push(json[i].back_expire_fee)
							back_packets.push(json[i].back_packet)
							back_expire_packets.push(json[i].back_expire_packet)
						}
						
						_data.back_amount = back_amounts.join(',')
						_data.back_expire_fee = back_expire_fees.join(',')
						_data.back_packet = back_packets.join(',')
						_data.back_expire_packet = back_expire_packets.join(',')
					}
					
					
					json = JSON.stringify(json)
					_data.back_config = json
					
					
					
					$.post(_url, _data, function(res, status) {
						if (status == 'success') {
							if (res.code == 0) {
								layer.alert('操作成功', function() {
									parent.layer.closeAll();
									parent.layui.table.reload('test-table-operate');
								});
							} else {
								layer.msg(res.msg);
								layer.closeAll('loading');
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}

				window.createnum = function() {

					var pos_sn_pre = $('input[name=pos_sn_pre]').val(),
						sn_start = $('input[name=sn_start]').val(),
						sn_end = $('input[name=sn_end]').val();

					if (!pos_sn_pre) {
						layer.msg('请输入机身编号')
					} else if (!sn_start || sn_start.length > 4 || !zorenumReg.test(Number(sn_start))) {
						console.log(zorenumReg.test(sn_start));
						layer.msg('开始序列号必须为小于4位的纯数字')
					} else if (!sn_end || sn_end.length > 4 || !zorenumReg.test(Number(sn_end))) {
						layer.msg('结束序列号必须为小于4位的纯数字')
					} else if (sn_start >= sn_end) {
						layer.msg('结束序列号必须大于等于开始序列号')
					} else {
						var numlength = Number(sn_end) - Number(sn_start),
							pos_sn_html = ''
							
						for (var i = 0; i <= numlength; i++) {
							var pos_sn = String(Number(sn_start) + Number(i))
							pos_sn_html += (pos_sn_pre + PrefixInteger(pos_sn,4) + ',\n').replace(/ /g,'')
							/* pos_sn_arr.push({
								'pos_sn': pos_sn_pre + PrefixInteger(pos_sn,4)
							}) */
						}
						$('textarea[name=pos_sn]').val(pos_sn_html)
					}

				}



				function initdata() {
					$.get(server.pos_detail, _data, function(res, status) {
						if (status == 'success') {
							if (res.code == 0) {
								var data = res.shop_member_pos
								var data1 = res.shop_member_pos.data

								var cathtml = '<option value="">全部</option>';
								for (var i = 0; i < data1.length; i++) {
									cathtml += '<option value=' + data1[i].id + '>' + data1[i].name + '</option>'
								}
								$('.catid').append(cathtml)

								form.val("test_detail", data.data);
								form.render();

								data.data.thumbnail ? createli(data.data.thumbnail, 'showimg', '1') : ''
								layedit.setContent(index, data.data.content)

							} else {
								layer.msg(res.msg);
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}
				
				
				
				var is1 = 1;
				var astime = 1;
				var dstime = 30;
				$('#table1').on('click',function(){
					is1++;
					let ass = (is1 - 1) * 30 + 1;
					let dss = dstime * is1;
					
					let html = `
						<tr class="tableli1">
							<td>
								<input name="oid" type="number" disabled="disabled" autocomplete="off" class="layui-input" value="${is1}">
							</td>
							<td>
								<input name="amount" type="number" placeholder="请输入达标金额" autocomplete="off" class="layui-input">
							</td>
							<td>
								<input name="back_amount" type="number" placeholder="请输入返现金额" autocomplete="off" class="layui-input">
							</td>
							<td>
								<input name="back_expire_fee" type="number" placeholder="请输入到期返现金额" autocomplete="off" class="layui-input">
							</td>
							<td>
								<input name="back_packet" type="number" placeholder="请输入返现红包" autocomplete="off" class="layui-input">
							</td>
							<td>
								<input name="back_expire_packet" type="number" placeholder="请输入到期返现红包" autocomplete="off" class="layui-input">
							</td>
							<td>
								<input name="startday" type="number" placeholder="请输入开始时间" autocomplete="off" class="layui-input" value="${ass}">
							</td>
							<td>
								<input name="endday" type="number" placeholder="请输入截止时间" autocomplete="off" class="layui-input" value="${dss}">
							</td>
							<td>
								<span class="deltext deltext1">-</span>
							</td>
						</tr>
					`
					
					$('.deltext1').css('display','none')
					$('.tableli1:last').after(html)
					
					
					$(".deltext1").unbind("click");
					$(".deltext1").each(function(i){
						$(this).click(function(){
							$(this).parent().parent().remove();
							is1 = $('.tableli1').length
							let list = $('.tableli1')
							if(is1 != 1){
								for(let i = 0; i < is1; i++){
									$(list[i]).find('td:first').html(i + 1)
								}
							}
							
							
							$('.deltext:last').css('display','block')
						});
					});
				})

			});
		</script>
	</body>
</html>
